﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class InvoiceDetailDA : IInvoiceDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string INVOICE_AMOUNT = "@INVOICE_AMOUNT";
        private string INVOICE_ID = "@INVOICE_ID";
        private string LINE_ID = "@LINE_ID";
        private string PAY_AMOUNT = "@PAY_AMOUNT";
        private string PAY_METHOD = "@PAY_METHOD";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_INVOICE_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@INVOICE_DETAIL WHERE INVOICE_ID = @INVOICE_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_INVOICE_DETAIL = " DELETE FROM @_@INVOICE_DETAIL WHERE INVOICE_ID = @INVOICE_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_INVOICE_DETAIL_ALL = " DELETE FROM @_@INVOICE_DETAIL WHERE INVOICE_ID = @INVOICE_ID ";
        private string SQL_INSERT_INVOICE_DETAIL = " INSERT INTO @_@INVOICE_DETAIL ( WH_ID, INVOICE_ID, LINE_ID, DESCR, STATUS, STATEMENT_ID, INVOICE_AMOUNT, PAY_AMOUNT, PAY_METHOD, REMARK, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @INVOICE_ID, @LINE_ID, @DESCR, @STATUS, @STATEMENT_ID, @INVOICE_AMOUNT, @PAY_AMOUNT, @PAY_METHOD, @REMARK, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_INVOICE_DETAIL = " SELECT  ROWID, WH_ID, INVOICE_ID, LINE_ID, DESCR, STATUS, STATEMENT_ID, INVOICE_AMOUNT, PAY_AMOUNT, PAY_METHOD, REMARK, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@INVOICE_DETAIL ";
        private string SQL_SELECT_ALL_INVOICE_DETAIL_LANGUAGE = " SELECT  ID.ROWID, ID.WH_ID, ID.INVOICE_ID, ID.LINE_ID, ID.DESCR, ID.STATUS, ID.STATEMENT_ID, ID.INVOICE_AMOUNT, ID.PAY_AMOUNT, ID.PAY_METHOD, ID.REMARK, ID.TS, ID.CREATED_BY, ID.CREATED_DATE, ID.UPDATED_BY, ID.UPDATED_DATE,T.[TEXT] AS STATUS_D FROM  @_@INVOICE_DETAIL ID  LEFT JOIN @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = ID.STATUS AND SCD.SYS_CODE_TYPE = 'InvoiceDetailStatus'  LEFT JOIN sysadm.TRANSLATION T ON 'InvoiceDetailStatus.'+ID.STATUS = T.ITEM_ID  AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL'  AND  T.LANG+'-'+T.COUNTRY = @LANGUAGE ";
        private string SQL_UPDATE_INVOICE_DETAIL = " UPDATE @_@INVOICE_DETAIL SET WH_ID = @WH_ID, DESCR = @DESCR, STATUS = @STATUS, STATEMENT_ID = @STATEMENT_ID, INVOICE_AMOUNT = @INVOICE_AMOUNT, PAY_AMOUNT = @PAY_AMOUNT, PAY_METHOD = @PAY_METHOD, REMARK = @REMARK, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE INVOICE_ID = @INVOICE_ID AND LINE_ID = @LINE_ID  ";
        private string STATEMENT_ID = "@STATEMENT_ID";
        private string STATUS = "@STATUS";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckInvoiceDetailIDUnique(string invoiceDetailID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INVOICE_ID", invoiceDetailID), new SqlParameter("@LINE_ID", lineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_INVOICE_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckInvoiceDetailIDUnique(DataBase dataBase, DbTransaction tran, string invoiceDetailID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INVOICE_ID", invoiceDetailID), new SqlParameter("@LINE_ID", lineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_INVOICE_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteInvoiceDetail(List<string> invoiceID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < invoiceID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INVOICE_ID", invoiceID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_INVOICE_DETAIL_ALL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteInvoiceDetail(List<string> invoiceID, List<string> lineID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < invoiceID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INVOICE_ID", invoiceID[j]), new SqlParameter("@LINE_ID", lineID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_INVOICE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteInvoiceDetail(DataBase dataBase, DbTransaction tran, List<string> invoiceID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < invoiceID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INVOICE_ID", invoiceID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_INVOICE_DETAIL_ALL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteInvoiceDetail(DataBase dataBase, DbTransaction tran, List<string> invoiceID, List<string> lineID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < invoiceID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INVOICE_ID", invoiceID[j]), new SqlParameter("@LINE_ID", lineID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_INVOICE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllInvoiceDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_INVOICE_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllInvoiceDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_INVOICE_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public InvoiceDetailInfo GetInvoiceDetailByID(string invoiceID, string lineID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_INVOICE_DETAIL_LANGUAGE.Replace("@_@", whLoginID) + " WHERE INVOICE_ID = @INVOICE_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INVOICE_ID", invoiceID), new SqlParameter("@LINE_ID", lineID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            InvoiceDetailInfo invoiceDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    invoiceDetailInfo = new InvoiceDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["INVOICE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["DESCR"].ToString(), reader["STATUS"].ToString(), reader["STATEMENT_ID"].ToString(), (reader["INVOICE_AMOUNT"].ToString() != "") ? decimal.Parse(reader["INVOICE_AMOUNT"].ToString()) : 0M, (reader["PAY_AMOUNT"].ToString() != "") ? decimal.Parse(reader["PAY_AMOUNT"].ToString()) : 0M, reader["PAY_METHOD"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["STATUS_D"].ToString(), 0M);
                }
            }
            return invoiceDetailInfo;
        }

        public InvoiceDetailInfo GetInvoiceDetailByID(DataBase dataBase, DbTransaction tran, string invoiceID, string lineID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_INVOICE_DETAIL_LANGUAGE.Replace("@_@", whLoginID) + " WHERE INVOICE_ID = @INVOICE_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INVOICE_ID", invoiceID), new SqlParameter("@LINE_ID", lineID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            InvoiceDetailInfo invoiceDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                invoiceDetailInfo = new InvoiceDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["INVOICE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["DESCR"].ToString(), reader["STATUS"].ToString(), reader["STATEMENT_ID"].ToString(), (reader["INVOICE_AMOUNT"].ToString() != "") ? decimal.Parse(reader["INVOICE_AMOUNT"].ToString()) : 0M, (reader["PAY_AMOUNT"].ToString() != "") ? decimal.Parse(reader["PAY_AMOUNT"].ToString()) : 0M, reader["PAY_METHOD"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["STATUS_D"].ToString(), 0M);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return invoiceDetailInfo;
        }

        public DataSet GetInvoiceDetailByQueryList(List<string> sqlWhere, InvoiceDetailQueryEntity invoicedetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_INVOICE_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!invoicedetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, invoicedetailQuery.CurrentPage, invoicedetailQuery.PageSize, invoicedetailQuery.SortField, invoicedetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetInvoiceDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, InvoiceDetailQueryEntity invoicedetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_INVOICE_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, invoicedetailQuery.CurrentPage, invoicedetailQuery.PageSize, invoicedetailQuery.SortField, invoicedetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<InvoiceDetailInfo> GetInvoiceDetailListByID(string invoiceID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_INVOICE_DETAIL_LANGUAGE.Replace("@_@", whLoginID) + " WHERE INVOICE_ID = @INVOICE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INVOICE_ID", invoiceID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            InvoiceDetailInfo invoiceDetailInfo = null;
            List<InvoiceDetailInfo> invoiceDetailListInfo = new List<InvoiceDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    invoiceDetailInfo = new InvoiceDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["INVOICE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["DESCR"].ToString(), reader["STATUS"].ToString(), reader["STATEMENT_ID"].ToString(), (reader["INVOICE_AMOUNT"].ToString() != "") ? decimal.Parse(reader["INVOICE_AMOUNT"].ToString()) : 0M, (reader["PAY_AMOUNT"].ToString() != "") ? decimal.Parse(reader["PAY_AMOUNT"].ToString()) : 0M, reader["PAY_METHOD"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["STATUS_D"].ToString(), 0M);
                    invoiceDetailListInfo.Add(invoiceDetailInfo);
                }
            }
            return invoiceDetailListInfo;
        }

        public List<InvoiceDetailInfo> GetInvoiceDetailListByID(DataBase dataBase, DbTransaction tran, string invoiceID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_INVOICE_DETAIL_LANGUAGE.Replace("@_@", whLoginID) + " WHERE INVOICE_ID = @INVOICE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@INVOICE_ID", invoiceID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            InvoiceDetailInfo invoiceDetailInfo = null;
            List<InvoiceDetailInfo> invoiceDetailListInfo = new List<InvoiceDetailInfo>();
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                invoiceDetailInfo = new InvoiceDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["INVOICE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["DESCR"].ToString(), reader["STATUS"].ToString(), reader["STATEMENT_ID"].ToString(), (reader["INVOICE_AMOUNT"].ToString() != "") ? decimal.Parse(reader["INVOICE_AMOUNT"].ToString()) : 0M, (reader["PAY_AMOUNT"].ToString() != "") ? decimal.Parse(reader["PAY_AMOUNT"].ToString()) : 0M, reader["PAY_METHOD"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["STATUS_D"].ToString(), 0M);
                invoiceDetailListInfo.Add(invoiceDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return invoiceDetailListInfo;
        }

        public int InsertInvoiceDetail(InvoiceDetailInfo invoiceDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    invoiceDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_InvoiceDetail_Parameters(invoiceDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_INVOICE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertInvoiceDetail(DataBase dataBase, DbTransaction tran, InvoiceDetailInfo invoiceDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    invoiceDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_InvoiceDetail_Parameters(invoiceDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_INVOICE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_InvoiceDetail_Parameters(InvoiceDetailInfo invoiceDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.INVOICE_ID, SqlDbType.VarChar, 30), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.DESCR, SqlDbType.VarChar, 50), new SqlParameter(this.STATUS, SqlDbType.VarChar, 30), new SqlParameter(this.STATEMENT_ID, SqlDbType.VarChar, 30), new SqlParameter(this.INVOICE_AMOUNT, SqlDbType.Decimal, 13), new SqlParameter(this.PAY_AMOUNT, SqlDbType.Decimal, 13), new SqlParameter(this.PAY_METHOD, SqlDbType.VarChar, 30), new SqlParameter(this.REMARK, SqlDbType.VarChar, 300), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(invoiceDetailInfo.WhID))
            {
                paramArray[0].Value = invoiceDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = invoiceDetailInfo.InvoiceID;
            paramArray[2].Value = invoiceDetailInfo.LineID;
            paramArray[3].Value = invoiceDetailInfo.Descr;
            paramArray[4].Value = invoiceDetailInfo.Status;
            paramArray[5].Value = invoiceDetailInfo.StatementID;
            paramArray[6].Value = invoiceDetailInfo.InvoiceAmount;
            paramArray[7].Value = invoiceDetailInfo.PayAmount;
            if (!string.IsNullOrEmpty(invoiceDetailInfo.PayMethod))
            {
                paramArray[8].Value = invoiceDetailInfo.PayMethod;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(invoiceDetailInfo.Remark))
            {
                paramArray[9].Value = invoiceDetailInfo.Remark;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            paramArray[10].Value = invoiceDetailInfo.CreatedBy;
            paramArray[11].Value = invoiceDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateInvoiceDetail(InvoiceDetailInfo invoiceDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    invoiceDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_InvoiceDetail_Parameters(invoiceDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_INVOICE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateInvoiceDetail(DataBase dataBase, DbTransaction tran, InvoiceDetailInfo invoiceDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    invoiceDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_InvoiceDetail_Parameters(invoiceDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_INVOICE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

